package org.jupiter.blog.web.controller;

import jakarta.validation.Valid;
import org.jupiter.blog.api.dto.JupiterDictionaryDTO;
import org.jupiter.blog.api.dto.PageResponseDto;
import org.jupiter.blog.api.http.JupiterResult;
import org.jupiter.blog.service.service.JupiterDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典管理控制器
 */
@RestController
@RequestMapping("/dictionaries")
public class JupiterDictionaryController {

    @Autowired
    private JupiterDictionaryService dictionaryService;

    /**
     * 创建字典项
     */
    @PostMapping
    public JupiterResult<Void> createDictionary(@RequestBody @Valid JupiterDictionaryDTO dto) {
        dictionaryService.createDictionary(dto);
        return JupiterResult.success(null);
    }

    /**
     * 根据UUID获取字典项
     */
    @GetMapping("/{uuid}")
    public JupiterResult<JupiterDictionaryDTO> getDictionaryById(@PathVariable("uuid") String uuid) {
        return JupiterResult.success(dictionaryService.getById(uuid));
    }

    /**
     * 更新字典项
     */
    @PutMapping("/{uuid}")
    public JupiterResult<Void> updateDictionary(@PathVariable("uuid") String uuid, @RequestBody @Valid JupiterDictionaryDTO dto) {
        dictionaryService.updateDictionary(uuid, dto);
        return JupiterResult.success(null);
    }

    /**
     * 删除字典项
     */
    @DeleteMapping("/{uuid}")
    public JupiterResult<Void> deleteDictionary(@PathVariable("uuid") String uuid) {
        dictionaryService.deleteDictionary(uuid);
        return JupiterResult.success(null);
    }

    /**
     * 分页查询字典列表
     */
    @PostMapping("/page")
    public JupiterResult<PageResponseDto<JupiterDictionaryDTO>> getDictionariesByPage(@RequestBody JupiterDictionaryDTO queryDTO) {
        List<JupiterDictionaryDTO> dictionaries = dictionaryService.getByPage(queryDTO);
        long total = dictionaryService.getCount(queryDTO);
        return JupiterResult.page(dictionaries, total);
    }

    /**
     * 获取所有字典项
     */
    @GetMapping
    public JupiterResult<List<JupiterDictionaryDTO>> getAllDictionaries() {
        return JupiterResult.success(dictionaryService.getAll());
    }

    /**
     * 根据字典类型获取字典列表
     */
    @GetMapping("/type/{dictType}")
    public JupiterResult<List<JupiterDictionaryDTO>> getDictionariesByType(@PathVariable("dictType") String dictType) {
        return JupiterResult.success(dictionaryService.getByDictType(dictType));
    }

    /**
     * 根据父级UUID获取子字典列表
     */
    @GetMapping("/parent/{parentUuid}")
    public JupiterResult<List<JupiterDictionaryDTO>> getDictionariesByParent(@PathVariable("parentUuid") String parentUuid) {
        return JupiterResult.success(dictionaryService.getByParentUuid(parentUuid));
    }

    /**
     * 根据字典类型和键获取字典项
     */
    @GetMapping("/type/{dictType}/key/{dictKey}")
    public JupiterResult<JupiterDictionaryDTO> getDictionaryByTypeAndKey(
            @PathVariable("dictType") String dictType,
            @PathVariable("dictKey") String dictKey) {
        return JupiterResult.success(dictionaryService.getByDictTypeAndKey(dictType, dictKey));
    }

    /**
     * 获取激活状态的字典列表
     */
    @GetMapping("/active/type/{dictType}")
    public JupiterResult<List<JupiterDictionaryDTO>> getActiveDictionariesByType(@PathVariable("dictType") String dictType) {
        return JupiterResult.success(dictionaryService.getActiveByDictType(dictType));
    }
}
